package com.university.mentalhealthplatform.global.log.manager;

import com.university.mentalhealthplatform.global.log.dto.IdRequest;
import com.university.mentalhealthplatform.global.log.dto.ManagerLogDTO;
import com.university.mentalhealthplatform.utils.PagedResponse;
import com.university.mentalhealthplatform.utils.stringUtils.ConvertEmptyToNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/manager-logs")
public class ManagerLogController {

    @Autowired
    private ManagerLogService managerLogService;

    /**
     * 查询日志（支持动态条件和分页）
     *
     * @param systemModule    模块名称（可选）
     * @param operatorName    操作人员名称（可选）
     * @param logStatus       操作状态（可选）
     * @param operationStatus 操作状态（可选）
     * @param startDate       起始日期（可选）
     * @param endDate       结束日期（可选）
     * @param page            当前页码（从 1 开始）
     * @param pageSize        每页大小
     * @return 分页结果
     */
    @GetMapping
    public ResponseEntity<PagedResponse<ManagerLogDTO>> getManagerLogs(
            @RequestParam(required = false) @ConvertEmptyToNull String systemModule,
            @RequestParam(required = false) @ConvertEmptyToNull String operatorName,
            @RequestParam(required = false) @ConvertEmptyToNull String logStatus,
            @RequestParam(required = false) @ConvertEmptyToNull String operationStatus,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
            @RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String sortField,
            @RequestParam(required = false) String sortOrder) {
        PagedResponse<ManagerLogDTO> response = managerLogService.getManagerLogs(
                systemModule,
                operatorName,
                logStatus,
                operationStatus,
                startDate,
                endDate,
                page-1,
                pageSize,
                sortField,
                sortOrder
        );



        return ResponseEntity.ok(response);
    }

    @PostMapping("/delete")
    public ResponseEntity<Void> deleteLoginLogsByIds(@RequestBody IdRequest request) {
        managerLogService.deleteLoginLogsByIds(request.getIds().toArray(new Long[0]));
        return ResponseEntity.noContent().build();
    }


    @PostMapping("/clear")
    public ResponseEntity<Void> clearAllLoginLogs() {
        managerLogService.clearAllLoginLogs();
        return ResponseEntity.noContent().build();
    }
}
